package pcloud.client.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.UUID;

public class ClientIdentifier
{
	private static boolean isFirstCreate = false; 
	private static String id = null;
	
	public static String getID()
	{
		if (id!=null) {
			return id;
		}
		
		String idFilePath = GlobalConfig.getInstance().getProperty("client");
		File idFile = new File(idFilePath);
		try {
			BufferedReader idReader = new BufferedReader(
					(new FileReader(idFile)));
			id = idReader.readLine();
			idReader.close();
			return id;
		} catch (FileNotFoundException e) {
			isFirstCreate = true;
			try {
				idFile.createNewFile();
				id = UUID.randomUUID().toString();
				BufferedWriter idWriter = new BufferedWriter(new FileWriter(
						idFile));
				idWriter.write(id);
				idWriter.flush();
				idWriter.close();
				return id;
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		} catch (IOException e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static boolean isFirstCreate()
	{
		if (id==null) {
			getID();
		}
		return isFirstCreate;
	}

}
